<template>
	<view class="oa-content">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar fixed customBack :bottomShadow="false" backgroundColor="#FFFFFF">
			<view slot="back" class='tn-custom-nav-bar__back' @click="goBack">
				<text class='icon tn-icon-left-arrow'></text>
			</view>
			<view class="tn-flex tn-flex-col-center tn-flex-row-center ">
				<text class="tn-text-bold tn-text-xl tn-color-black">添加客户</text>
			</view>
		</tn-nav-bar>

		<view class="" :style="{paddingTop: vuex_custom_bar_height + 30 + 'px'}" style="padding-bottom: 240rpx;">
			<add @submitForm='submitForm' title="客户" />

		</view>
	</view>
</template>

<script>
	import Add from '@/crm/components/customer-add-vue.vue'
	import template_page_mixin from '@/libs/mixin/template_page_mixin.js'
	import {
		addCustomer
	} from '@/api/crm/customer.js'
	export default {
		name: 'CustomerAdd',
		mixins: [template_page_mixin],
		components: {
			Add
		},
		data() {
			return {


				// 表单数据
				formData: {
					name: '', // 客户名称
					source: '', // 客户来源
					mobile: '', // 手机号码
					telephone: '', // 固定电话
					email: '', // 邮箱地址
					level: '', // 客户级别
					industryId: '', // 客户行业
					contactNextTime: '', // 下次联系时间
					detailAddress: '', // 详细地址
					remark: '', // 备注信息
					lockStatus: '', // 锁定状态
					dealStatus: '' // 成交状态
				}
			}
		},
		onLoad() {
			console.log('USER_ID', this.USER_ID)
			console.log('1212', JSON.stringify(this.DICT.crm_customer_source))
		},
		methods: {

			// 提交表单
			submitForm(formData) {
				this.formData = formData
				// 验证必填字段
				if (!this.formData.name.trim()) {
					uni.showToast({
						title: '请输入客户名称',
						icon: 'none'
					})
					return
				}

				if (!this.formData.source) {
					uni.showToast({
						title: '请选择客户来源',
						icon: 'none'
					})
					return
				}

				// 验证手机号格式
				if (this.formData.mobile && !/^1[3-9]\d{9}$/.test(this.formData.mobile)) {
					uni.showToast({
						title: '手机号格式不正确',
						icon: 'none'
					})
					return
				}

				// 验证邮箱格式
				if (this.formData.email && !/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(this.formData.email)) {
					uni.showToast({
						title: '邮箱格式不正确',
						icon: 'none'
					})
					return
				}

				// 显示加载提示
				uni.showLoading({
					title: '提交中...'
				})

				// 准备提交数据
				const submitData = {
					name: this.formData.name.trim(),
					source: this.formData.source,
					mobile: this.formData.mobile || null,
					telephone: this.formData.telephone || null,
					email: this.formData.email || null,
					level: this.formData.level || null,
					industryId: this.formData.industryId || null,
					contactNextTime: this.formData.contactNextTime ? new Date(this.formData.contactNextTime +
							'T00:00:00')
						.getTime() : null,
					detailAddress: this.formData.detailAddress || null,
					remark: this.formData.remark || null,
					lockStatus: this.formData.lockStatus || '0',
					dealStatus: this.formData.dealStatus || '0',
					ownerUserId: this.USER_ID
				}

				// 调用API接口
				console.log('提交数据:', submitData)
				this.api.post('/crm/customer/create', submitData).then(response => {
					uni.showToast({
						title: '客户添加成功',
						icon: 'success'
					})
					// 延迟返回上一页
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 60%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.15);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		color: #FFFFFF;
		font-size: 32rpx;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

	}

	.oa-content {
		max-width: 640px;
		margin: 0 auto;
		min-height: 100vh;
		padding-bottom: 60rpx;
		padding-bottom: calc(80rpx + env(safe-area-inset-bottom) / 2);
		padding-bottom: calc(80rpx + constant(safe-area-inset-bottom));
	}

	/* 间隔线 start*/
	.tn-strip-bottom-min {
		width: 100%;
		border-bottom: 1rpx solid #F8F9FB;
	}

	.tn-strip-top {
		width: 100%;
		border-top: 20rpx solid rgba(241, 241, 241, 0.8);
	}

	/* 间隔线 end*/

	/* 底部悬浮按钮 start*/
	.tn-tabbar-height {
		min-height: 160rpx;
		height: calc(180rpx + env(safe-area-inset-bottom) / 2);
		height: calc(180rpx + constant(safe-area-inset-bottom));
	}

	.tn-footerfixed {
		max-width: 640px;
		margin: 0 auto;

		width: 100%;
		bottom: calc(40rpx + env(safe-area-inset-bottom));
		z-index: 1024;
		box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0);
	}

	/* 底部悬浮按钮 end*/
</style>